package com.xywl.escort.view.evaluate

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.EvaluateAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.CommentsReq
import com.xywl.escort.bean.CommentsResp
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_product_evaluate.*
import java.util.*
import javax.inject.Inject

@Route(path = "/xy/product/evaluate")
class ProductEvaluateActivity : BaseActivity(), ProductEvaluateContract.View, SwipeRefreshLayout.OnRefreshListener {
    private lateinit var mAdapter: EvaluateAdapter
    private val mDataBeanList = ArrayList<CommentsResp.DataBean.ListBean>()
    @Autowired
    @JvmField
    var spuId: String = ""

    private var pageNum = 1
    private var isEnd: Boolean = false

    @Inject
    lateinit var mPresenter: ProductEvaluateContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_evaluate)
        ARouter.getInstance().inject(this)

        initView()
        initData()
    }

    private fun initView() {
        showBackButton()
        setTitleText("商品评论")

        mAdapter = EvaluateAdapter(R.layout.item_evaluate, mDataBeanList)
        recycler_evaluate.layoutManager = LinearLayoutManager(this)
        recycler_evaluate.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        val tvEmpty = emptyView.findViewById<TextView>(R.id.tv_empty)
        tvEmpty.text = "暂无评论"
        mAdapter.emptyView = emptyView

        swipeLayout.setOnRefreshListener(this)
        swipeLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.aid_prompt_color, R.color.colorBackground)

        mAdapter.setOnLoadMoreListener({
            swipeLayout.isEnabled = false
            recycler_evaluate.postDelayed({
                swipeLayout.isEnabled = false
                if (isEnd) {
                    //数据全部加载完毕
                    mAdapter.loadMoreEnd()
                    swipeLayout.isEnabled = true
                } else {
                    //成功获取更多数据
                    val req = CommentsReq()
                    req.pageNum = pageNum
                    req.spuId = spuId
                    mPresenter.loadComments(req)
                }
            }, 200)
        }, recycler_evaluate)
    }

    private fun initData() {
        mPresenter.takeView(this)
        pageNum = 1
        val req = CommentsReq()
        req.pageNum = pageNum
        req.spuId = spuId
        mPresenter.loadComments(req)
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun onRefresh() {
        mAdapter.setEnableLoadMore(false)
        pageNum = 1
        val req = CommentsReq()
        req.pageNum = pageNum
        req.spuId = spuId
        mPresenter.loadComments(req)
    }

    override fun loadCommentsSuccess(resp: CommentsResp?) {
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_evaluate.visibility = View.VISIBLE
        if (pageNum == 1) {
            mDataBeanList.clear()
        }
        if (pageNum >= resp!!.data.pages) {
            isEnd = true
        } else {
            isEnd = false
            pageNum += 1
        }
        mDataBeanList.addAll(resp.data.list)
        mAdapter.setNewData(mDataBeanList)
        mAdapter.loadMoreComplete()
        swipeLayout.isEnabled = true
    }

    override fun loadCommentsFailure() {
        progress_bar.visibility = View.GONE
        swipeLayout.isRefreshing = false
        recycler_evaluate.visibility = View.VISIBLE
    }
}
